package simri.servidor.negocio.sensor;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import simri.sensor.LeituraSensor;
import simri.servidor.jdbc.ConexaoBD;
import simri.servidor.jdbc.GlobalApp;

public class ColLeituraSensor {
	
	public ColLeituraSensor(){}
	
	public String cadLeitura(LeituraSensor ltSensor) throws Exception{
		
		ConexaoBD conexaoBDL = GlobalApp.getConexaoBD();
		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2012-05-17 11:53:23	
		
		
		StringBuffer sqlR = new StringBuffer("INSERT INTO leituraSensor");
		sqlR.append(" (idleituraSensor, valorLeitura, dtLeitura, sensor_idsensor) VALUES (");
		sqlR.append("" + null + ", ");
		sqlR.append("'" + ltSensor.getValorLeitura().trim() + "',");
		sqlR.append("'"+ f.format(ltSensor.getDataLeitura().getTime()) + "',");
		sqlR.append("" + ltSensor.getIdSensor() + ")");
		conexaoBDL.execSQL(sqlR.toString());
		
		return "Leitura Gravada!";
		
	}
	
	public boolean validaLeitura(LeituraSensor pkLeitura){
		
		if(pkLeitura.getValorLeitura()==""){ return false; }
		if(pkLeitura.getIdSensor()==0){ return false; }
		
		try {
			ConexaoBD conexaoBDL = GlobalApp.getConexaoBD();
			ResultSet resultR = conexaoBDL.execSelect("SELECT * FROM sensor WHERE idsensor = " + pkLeitura.getIdSensor());
			if(!resultR.next()){return false;}
			
		} catch (Exception e) {
			System.out.println("Erro ao validar leitura do sensor.");
		}
		
		return true;
	}
	
	public void consultaLeituraSensor(int idSensor){
		
		ConexaoBD conexaoBDL;
		try {
			conexaoBDL = GlobalApp.getConexaoBD();
			
			LeituraSensor lt = null;
			
			ResultSet resultR = conexaoBDL.execSelect("SELECT * FROM leituraSensor WHERE sensor_idSensor = " + idSensor);
			if(resultR.next()){
				lt = new LeituraSensor();
				lt.setDataLeitura(resultR.getTimestamp("dtLeitura"));
				lt.setValorLeitura(resultR.getString("valorLeitura"));
				lt.setIdLeitura(resultR.getInt("idleituraSensor"));
				lt.setIdSensor(resultR.getInt("sensor_idsensor"));
			}
			resultR.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
	}
	
	public ArrayList<ArrayList<String>> listaLeituraSensor(String valor, int tipo){
		
		ArrayList<ArrayList<String>> tabela = new ArrayList<ArrayList<String>>();
		ConexaoBD bd;
		try {
			bd = GlobalApp.getConexaoBD();
			
			StringBuilder s = new StringBuilder();
			s.append("SELECT * FROM leituraSensor WHERE ");

			switch (tipo){
			case 0:s.append(String.format("sensor_idsensor=%s", valor)); break;
			case 1:s.append("valorLeitura LIKE '%"+valor+"%'"); break;
			case 2:s.append("DATE(dtLeitura) = '"+valor+"'"); break;
			}
			
			SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

			ResultSet retornoConsulta = bd.execSelect(s.toString());	

			while (retornoConsulta.next()){
				ArrayList<String> linha = new ArrayList<String>();
				linha.add(retornoConsulta.getString("idleituraSensor"));
				linha.add(retornoConsulta.getString("sensor_idsensor"));
				linha.add(retornoConsulta.getString("valorLeitura"));
				Date dt = new Date(retornoConsulta.getTimestamp("dtLeitura").getTime());
				linha.add(f.format(dt));
				tabela.add(linha);
			}
			retornoConsulta.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tabela;
	}
		
}
	

